<?php

class RecuperacionController extends Zend_Controller_Action {

    public function init() {
        X\Permiso::paraController();
        $this->em = Zend_Registry::getInstance()->entitymanager;
    }

    public function indexAction() {
        X\Permiso::paraAction('Listar.Recuperacion');
        $qb = $this->em->createQueryBuilder();

        $profesores = $qb
                        ->select('p.idProfesor')
                        ->addselect('CONCAT(CONCAT(u.apellido,\', \'),u.nombre) as nombre')
                        ->from('Default_Model_Profesor', 'p')
                        ->join('p.usuario','u')
                        ->orderBy('u.apellido', 'asc')
                        ->addOrderBy('u.nombre', 'asc')
                        ->getQuery()
                        ->execute();
        $materias = $this->em->createQueryBuilder()
                        ->select('m.idMateria')
                        ->addselect('m.nombre')
                        ->from('Default_Model_Materia', 'm')
                        ->orderBy('m.nombre', 'asc')
                        ->getQuery()
                        ->execute();

        $this->view->profesores = Zend_Json::encode(Array(
            "success" => true,
            "total" => count($profesores),
            "rows" => $profesores
        ));
        $this->view->materias = Zend_Json::encode(Array(
            "success" => true,
            "total" => count($materias),
            "rows" => $materias
        ));
    }

    public function recuperarAction(){
        X\Permiso::paraAction('Crear.Recuperacion');
        $materias = $this->em->createQueryBuilder()
            ->select  ('m.idMateria')
            ->addSelect  ('m.nombre')
            ->from       ('Default_Model_Curso', 'c')
            ->join       ('c.carreraMateria', 'cm')
            ->join       ('cm.materia', 'm')
            ->join       ('cm.carrera', 'ca')
            ->where("c.validoDesde <= '".date("Y-m-d")."'")
            ->andWhere("c.validoHasta >= '".date("Y-m-d")."'")
            ->groupBy('m.idMateria')
            ->orderBy('m.nombre', 'asc')
            ->getQuery()
            ->execute();

        $facultades = $this->em->createQueryBuilder()
            ->select('f.idFacultad')
            ->addselect('f.nombre')
            ->from('Default_Model_Facultad', 'f')
            ->orderBy('f.nombre', 'asc')
            ->getQuery()
            ->execute();

        $this->view->facultades = Zend_Json::encode(Array(
            "success" => true,
            "total" => count($facultades),
            "rows" => $facultades
        ));
        $this->view->materias = Zend_Json::encode(Array(
            "success" => true,
            "total" => count($materias),
            "rows" => $materias
        ));
    }
    public function ajaxGetSeccionesAction(){
        $materia = $this->_request->getPost("idMateria");

        $qb = $this->em->createQueryBuilder();
        $hoy = date("Y-m-d");

        $secciones = $qb
            ->select('c.seccion')
            ->from('Default_Model_Curso', 'c')
            ->join('c.carreraMateria','cm')
            ->join('cm.materia','m')
            ->where("m.idMateria = '".$materia."'")
            ->andWhere("c.validoDesde <= '".$hoy."'")
            ->andWhere("c.validoHasta >= '".$hoy."'")
            ->groupBy('c.seccion')
            ->orderBy('c.seccion')
            ->getQuery()
            ->execute();

        echo X\Msg::Escape(Zend_Json::encode(Array(
            "success" => true,
            "results" => count($secciones),
            "rows" => $secciones
        )));
    }
    public function ajaxGetCarrerasAction(){
        $seccion = $this->_request->getPost("seccion");
        $materia = $this->_request->getPost("idMateria");

        $qb = $this->em->createQueryBuilder();
        $hoy = date("Y-m-d");

        $carreras = $qb
            ->select('ca.nombre')
            ->from('Default_Model_Curso', 'c')
            ->join('c.carreraMateria','cm')
            ->join('cm.carrera','ca')
            ->where("cm.materia = '".$materia."'")
            ->andWhere("c.seccion = '".$seccion."'")
            ->andWhere("c.validoDesde <= '".$hoy."'")
            ->andWhere("c.validoHasta >= '".$hoy."'")
            ->groupBy('ca.idCarrera')
            ->orderBy('ca.nombre')
            ->getQuery()
            ->execute();

        echo X\Msg::Escape(Zend_Json::encode(Array(
            "success" => true,
            "results" => count($carreras),
            "rows" => $carreras
        )));
    }
}

